package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IShoppingGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.cskaoyan.mall.constant.ShoppingRetCode;

import javax.servlet.http.HttpServletRequest;

/**
 * oushao   2022-06-18    09:56
 */
@RestController
public class ShoppingGoodsController {


    @Autowired
    IShoppingGoodsService iShoppingGoodsService;

    @GetMapping("/shopping/goods")
    public ResponseData shoppingGoods(PageInfo pageInfo){

        AllProductRequest itemRequest = new AllProductRequest();
        itemRequest.setSort(pageInfo.getSort());
        itemRequest.setPage(pageInfo.getPage());
        itemRequest.setPriceGt(pageInfo.getPriceGt());
        itemRequest.setPriceLte(pageInfo.getPriceLte());
        itemRequest.setSize(pageInfo.getSize());

        AllProductResponse itemResponse = iShoppingGoodsService.getShoppingGoods(itemRequest);

        if(ShoppingRetCode.SUCCESS.getCode().equals(itemResponse.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(itemResponse.getProductDtoList());
            pageResponse.setTotal(itemResponse.getTotal());
            return new ResponseUtil<>().setData(pageResponse);

        }


        return new ResponseUtil<>().setErrorMsg(itemResponse.getMsg());
    }

    @GetMapping("/shopping/recommend")
    public ResponseData shoppingRecommend(HttpServletRequest request){

        RecommendResponse recommendResponse = iShoppingGoodsService.getShoppingRecommend();

        return new ResponseUtil<>().setData(recommendResponse.getPanelContentItemDtos());

    }

    @GetMapping("/shopping/product/{productId}")
    public ResponseData shoppingProduct(@PathVariable("productId") Long productId){

        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(productId);

        ProductDetailResponse response = iShoppingGoodsService.getShoppingProduct(request);

        return new ResponseUtil<>().setData(response.getProductDetailDto());

    }


}
